﻿using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.Contract.Dtos;
using NP.BPMReportPlatform.MaterialMasterData.Dtos;
using NP.BPMReportPlatform.RecurringJobs;
using NP.BPMReportPlatform.Reports.OrderMaterialDetail.Dtos;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.Contract
{
    [Authorize]
    [ApiController]
    [Route("api/ContractESign")]
    public class ContractESignController : BPMReportPlatformController
    {
        private readonly IContractESignService _service;
        private readonly IExcelImporter _importer;
        public ContractESignController(IContractESignService service, IExcelImporter importer)
        {
            _service = service;
            _importer = importer;
        }

        [HttpPost]
        [Route("GetAllAsync")]
        public Task<PagedResultDto<ContractESignDto>> GetAllAsync([FromBody] ContractESignInput input)
        {
            return _service.GetAllAsync(input);
        }

        [HttpPost]
        [Route("ExportAsync")]
        public async Task<XlsxFileResult<ContractESignDto>> ExportAsync([FromBody] ContractESignInput input)
        {
            var rst = await _service.ExportAsync(input);
            return new XlsxFileResult<ContractESignDto>(rst, "导出Excel");
        }

        [HttpPost]
        [Route("ImportAsync")]
        public async Task<ImportResultExt<ContractESignDto>> ImportAsync(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<ContractESignDto>
                {
                    Exception = new Exception("请上传文件！")
                };
                return res;
            }
            var stream = file.OpenReadStream();
            var rst = await _importer.Import<ContractESignDto>(stream);
            stream.Close();
            if (rst.HasError)
            {
                var resp = new ImportResultExt<ContractESignDto>()
                {
                    Exception = rst.Exception,
                    RowErrors = rst.RowErrors,
                    TemplateErrors = rst.TemplateErrors
                };
                return resp;
            }
            else
            {
                return await _service.ImportAsync(rst.Data.ToList());
            }
        }

        [HttpPost("EditAsync")]
        public async Task EditAsync([FromBody] ContratEsignEditInput dto)
        {
            await _service.EditAsync(dto);
        }

        [HttpDelete("Delete")]
        public async Task Delete([FromQuery] string id)
        {
            await _service.Delete(id);
        }

        [HttpGet("IsAdmin")]
        public async Task<bool> IsAdmin()
        {
            return await _service.IsAdmin();
        }
    }
}
